<?php


namespace app\game;


use app\game\node\AbsNode;
use app\game\node\NodeFood;
use app\game\node\NodeForm;
use app\game\node\NodeSnake;

class Snake
{

    const UP = 8;
    const DOWN = 2;
    const LEFT = 4;
    const RIGHT = 6;


    /**
     * 节点
     * @var
     */
    protected $nodes;

    /**
     * @var Form
     */
    protected $form;


    /**
     * 食物列表
     * @var FoodList
     */
    protected $foodList;

    /**
     * 方向
     * @var int
     */
    protected $direction;

    /**
     * @var int
     */
    protected $length;


    /**
     * @param FoodList $foodList
     */
    public function setFoodList($foodList)
    {
        $this->foodList = $foodList;
    }

    /**
     * @param Form $form
     */
    public function setForm($form)
    {
        $this->form = $form;

        // 初始化
        $this->nodes[] = new NodeSnake(5,5);
        $this->form->setRowColNode(5,5, $this->nodes[0]);
        $this->direction = self::RIGHT;
    }

    /**
     * @return AbsNode
     */
    protected function getHead()
    {
        return $this->nodes[0];
    }

    protected function positionDeal($row, $col)
    {
        $node = $this->form->getNode($row,$col);

        if ($node instanceof NodeForm){

            $newSnakeNode = new NodeSnake($row,$col);
            array_unshift($this->nodes,$newSnakeNode);
            /**
             * @var AbsNode $node
             */
            $node = array_pop($this->nodes);

            $this->form->setRowColNode($row,$col,$newSnakeNode);
            $this->form->setRowColNode($node->getRow(),$node->getCol(),new NodeForm($node->getRow(),$node->getCol()));
        }else if ($node instanceof NodeFood){  // 遇到食物则不删除尾巴, 且将食物列表中的食物移除

            $this->foodList->remove($node);
            $newSnakeNode = new NodeSnake($row,$col);
            array_unshift($this->nodes,$newSnakeNode);
            $this->form->setRowColNode($row,$col,$newSnakeNode);
        }else if ($node instanceof NodeSnake){ // 遇到自身则继续

            $newSnakeNode = new NodeSnake($row,$col);
            array_unshift($this->nodes,$newSnakeNode);
            /**
             * @var AbsNode $node
             */
            $node = array_pop($this->nodes);

            $this->form->setRowColNode($row,$col,$newSnakeNode);
            $this->form->setRowColNode($node->getRow(),$node->getCol(),new NodeForm($node->getRow(),$node->getCol()));
        }else{
            // 越界了, 什么都不是, 需要修改反方向

        }
    }

    public function getDirection()
    {
        $direction = file_get_contents('key.txt');

        if (in_array($direction,[self::UP,self::RIGHT,self::DOWN,self::LEFT])){
            $this->direction = $direction;
        }
    }

    protected function searchFood()
    {
        $firstFood = $this->foodList->getFirstFood();

        if (!$firstFood){
            return;
        }

        $snakeHead = $this->getHead();

        if ($firstFood->getRow() > $snakeHead->getRow()){
            $this->direction = self::DOWN;
        } else if ($firstFood->getRow() === $snakeHead->getRow()){
            // ...
        } else{
            $this->direction = self::UP;
        }

        if ($firstFood->getCol() > $snakeHead->getCol()){
            $this->direction = self::RIGHT;
        } else if($firstFood->getCol() === $snakeHead->getCol()){
            // ....
        } else{
            $this->direction = self::LEFT;
        }
    }

    public function move()
    {
        $head = $this->getHead();
        $row = $head->getRow();
        $col = $head->getCol();

        switch ($this->direction){
            case self::UP:
                $row--;
                break;
            case self::DOWN:
                $row++;
                break;
            case self::LEFT:
                $col--;
                break;
            case self::RIGHT:
                $col++;
                break;
        }

        $this->positionDeal($row,$col);
        $this->searchFood();
//        $this->getDirection();
    }

}